﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using SIR.Common.Condition;
using SIR.Entities.Implementations;
using SIR.Entities.Interfaces;
using SIR.Web.API;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Hosting;
using System.Web.Http;
using System.Web.Http.Hosting;

namespace SIR.Web.Services.Tests
{
	[TestClass]
	public class DataControllerTest
	{
		[TestInitialize]
		public void ConnectToDatabase()
		{
			// connect to the database
			DAL.DataServer.Connect(System.Configuration.ConfigurationManager.ConnectionStrings["SIR"].ConnectionString);
			// establish the HTTP context
			SimpleWorkerRequest request = new SimpleWorkerRequest("", "", "", null, new StringWriter());
			HttpContext context = new HttpContext(request);
			HttpContext.Current = context;
		}

		[TestCleanup]
		public void DisconnectFromTheDatabase()
		{
			// disconnect from the database
			DAL.DataServer.Disconnect();
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.ViewLibrary, 1, typeof(DataController))]
		public void ViewLibrary_AuthenticationFailed()
		{
			var authController = new AuthenticationController();
			// Attempt log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@sir.com", password = "password" });
			// Failed log in
			Assert.IsNotNull(authResponse);
			Assert.IsFalse(authResponse.Successful);
			// Create data controller
			var dataController = new DataController();
			// Download folders
			var dataResponse = dataController.Download("Folder", null);
			// Verify download failed because user wasn't authenticated
			Assert.IsNotNull(dataResponse);
			Assert.IsFalse(dataResponse.Successful);
			Assert.AreEqual("User must be authenticated", dataResponse.Message);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.ViewLibrary, 2, typeof(DataController))]
		public void ViewLibrary_ValidatePictureCount()
		{
			var authController = new AuthenticationController();
			// log in to the system
			var authResponse = authController.LogIn(new Credentials() { email = "matt@sir.com", password = "password" });
			// verify successful authentication
			Assert.IsNotNull(authResponse);
			Assert.AreEqual("Authentication Successful", authResponse.Message);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// download the folders for the user
			var dataResponse = dataController.Download("Folder",
				new Common.Condition.Predicate("UserId", authResponse.Content.Id, Common.Condition.PredicateOperator.EqualTo));
			// Verify the folders were downloaded
			Assert.IsNotNull(dataResponse);
			Assert.IsTrue(dataResponse.Successful);
			// get the library
			var library = dataResponse.Content.FirstOrDefault() as IFolder;
			// verifty the number of images in the library
			Assert.AreEqual((uint)10, library.GetNumberOfImagesInTree());
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.CreateFolder, 1, typeof(DataController))]
		public void CreateFolder_AuthenticationFailed()
		{
			var authController = new AuthenticationController();
			// Attempt to log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@sir.com", password = "password" });
			// Verify authentication failed
			Assert.IsNotNull(authResponse);
			Assert.IsFalse(authResponse.Successful);
			var dataController = new DataController();
			// Upload fake data
			var dataResponse = dataController.Upload(new UploadCriteria() { objectType = "", values = new List<KeyValuePair<string, string>>() });
			// Verify response of a failed attempt because the user was not authenticated
			Assert.IsNotNull(dataResponse);
			Assert.IsFalse(dataResponse.Successful);
			Assert.AreEqual("User must be authenticated", dataResponse.Message);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.CreateFolder, 2, typeof(DataController))]
		public void CreateFolder_NewFolderCreated()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@email.com", password = "password" });
			// Verify successful log in
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload a new folder to the data controller
			var dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>("ParentFolderId", authResponse.Content.Library.FolderId.ToString()),
				new KeyValuePair<string,string>("Name", "CreateFolder_NewFolderCreated")
			}
			});
			// Verify that the folder was created by a successful response
			Assert.IsNotNull(dataResponse);
			Assert.AreEqual(string.Empty, dataResponse.Message);
			Assert.IsTrue(dataResponse.Successful);
			// Delete the new folder
			var folder = dataResponse.Content as Folder;
			folder.IsDeleted = true;
			DAL.DataServer.Save(folder);
			// Verify the values of the folder match the uploaded values
			Assert.IsNotNull(folder);
			Assert.AreEqual(authResponse.Content.Id, folder.UserId);
			Assert.AreNotEqual(0, folder.ParentFolderId);
			Assert.AreEqual("CreateFolder_NewFolderCreated", folder.Name);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.CreateFolder, 3, typeof(DataController))]
		public void CreateFolder_NewFolderWithExistingName()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@email.com", password = "password" });
			// Verify successful log in
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload a folder
			var dataResponseA = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString()),
				new KeyValuePair<string,string>( "Name", "CreateFolder_NewFolderWithExistingName")}
			});
			// Upload another new folder
			var dataResponseB = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() {
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "CreateFolder_NewFolderWithExistingName" )}
			});
			// Upload a third new folder
			var dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "CreateFolder_NewFolderWithExistingName" )}
			});
			// Delete the first folder
			var folder = dataResponseA.Content as Folder;
			folder.IsDeleted = true;
			DAL.DataServer.Save(folder);
			// delete the second folder 
			folder = dataResponseB.Content as Folder;
			folder.IsDeleted = true;
			DAL.DataServer.Save(folder);
			// Verify the third upload was successful
			Assert.IsNotNull(dataResponse);
			Assert.AreEqual(string.Empty, dataResponse.Message);
			Assert.IsTrue(dataResponse.Successful);
			// Delete the third folder
			folder = dataResponse.Content as Folder;
			folder.IsDeleted = true;
			DAL.DataServer.Save(folder);
			// Verify the values of the third folder and that the name resolver change the name correctly
			Assert.IsNotNull(folder);
			Assert.AreEqual(authResponse.Content.Id, folder.UserId);
			Assert.AreNotEqual(0, folder.ParentFolderId);
			Assert.AreEqual("CreateFolder_NewFolderWithExistingName (3)", folder.Name);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.MovePictureFolder, 1, typeof(DataController))]
		public void MovePictureFolder_AuthenticationFailed()
		{
			var authController = new AuthenticationController();
			// Attempt to log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@sir.com", password = "password" });
			// Verify log in failed
			Assert.IsNotNull(authResponse);
			Assert.IsFalse(authResponse.Successful);
			var dataController = new DataController();
			// Try to upload data
			var dataResponse = dataController.Upload(new UploadCriteria() { objectType = "", values = new List<KeyValuePair<string, string>>() });
			// Verify that the upload failed because the authentication failed
			Assert.IsNotNull(dataResponse);
			Assert.IsFalse(dataResponse.Successful);
			Assert.AreEqual("User must be authenticated", dataResponse.Message);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.MovePictureFolder, 2, typeof(DataController))]
		public void MovePictureFolder_MoveFolderToNewFolderSuccessful()
		{
			var authController = new AuthenticationController();
			// Log In
			var authResponse = authController.LogIn(new Credentials() { email = "test@email.com", password = "password" });
			// Verify successful log in
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload multple folders
			var dataResponseA = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "MovePictureFolder_MoveFolderToNewFolderSuccessful" )}
			});
			// upload B under A
			var dataResponseB = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "MovePictureFolder_MoveFolderToNewFolderSuccessful (2)" )}
			});
			// move B to Library
			var dataResponseC = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "Id", dataResponseB.Content.Id.ToString() ),
				new KeyValuePair<string,string>( "ParentFolderId", dataResponseA.Content.Id.ToString() )},
			});
			// check that the folder move was uploaded successfully
			Assert.IsNotNull(dataResponseC);
			Assert.AreEqual(string.Empty, dataResponseC.Message);
			Assert.IsTrue(dataResponseC.Successful);
			// Delete the parent folder
			var parentFolder = dataResponseA.Content as Folder;
			parentFolder.IsDeleted = true;
			DAL.DataServer.Save(parentFolder);
			// Verify that the moved folder is now under the library
			var folder = dataResponseC.Content as Folder;
			Assert.AreNotEqual(authResponse.Content.Library.FolderId, folder.ParentFolderId);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.MovePictureFolder, 3, typeof(DataController))]
		public void MovePictureFolder_MovePictureToNewFolderSuccesfull()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@email.com", password = "password" });
			// Verify the successful authentication
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			// Upload the image
			var imageController = new ImageController();
			// Initalize the request
			imageController.Request = new System.Net.Http.HttpRequestMessage();
			imageController.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
			// Get the image path
			var fileName = Path.Combine(Directory.GetCurrentDirectory(), "..\\..\\..\\SIR.Web.Tests\\Content\\ChangePrivacySetting_Image.jpg");
			// Set the request file content
			var formDataContent = new MultipartFormDataContent();
			var fileContent = new StreamContent(new MemoryStream(System.IO.File.ReadAllBytes(fileName)));
			fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/JPEG");
			var folderId = authResponse.Content.Library.FolderId;
			var textContent = new StreamContent(new MemoryStream(System.Text.UnicodeEncoding.UTF8.GetBytes(folderId.ToString())));
			// add the conent to the form data request
			formDataContent.Add(fileContent, "\"file\"", fileName);
			formDataContent.Add(textContent, "\"folder\"", folderId.ToString());
			// set the request content
			imageController.Request.Content = formDataContent;
			// post the request and get the result
			var result = imageController.PostFormData().Result;
			// Get the uploaded image object and verify that the image was uploaded
			var content = ((ObjectContent<List<Response<Image>>>)(result.Content)).ReadAsAsync<List<Response<Image>>>().Result;
			Assert.IsNotNull(content);
			Assert.AreEqual(content[0].Message, "");
			Assert.IsTrue(content[0].Successful);

			var dataController = new DataController();
			// Upload a new folder
			var dataResponseA = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "MovePictureFolder_MovePictureToNewFolderSuccesfull" )}
			});
			// upload data to move the image to the new folder
			var dataResponseB = dataController.Upload(new UploadCriteria()
			{
				objectType = "Image",
				values = new List<KeyValuePair<string, string>>() {
				new KeyValuePair<string,string>( "Id", content[0].Content.Id.ToString() ),
				new KeyValuePair<string,string>( "FolderId", dataResponseA.Content.Id.ToString() )}
			});
			// Verify success on the image move response
			Assert.IsNotNull(dataResponseB);
			Assert.AreEqual(string.Empty, dataResponseB.Message);
			Assert.IsTrue(dataResponseB.Successful);
			// Delete the new folder
			var parentFolder = dataResponseA.Content as Folder;
			parentFolder.IsDeleted = true;
			DAL.DataServer.Save(parentFolder);
			// Verify that the image's folder is no longer the library
			var image = dataResponseB.Content as Image;
			Assert.AreNotEqual(authResponse.Content.Library.FolderId, image.FolderId);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.MovePictureFolder, 4, typeof(DataController))]
		public void MovePictureFolder_PictureMovedWithFolderMoveSuccesfull()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@email.com", password = "password" });
			// Verify successful aithentication
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload a new folder to the library
			var dataResponseA = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "MovePictureFolder_PictureMovedWithFolderMoveSuccesfull" )}
			});
			// Upload a second folder to the library
			var dataResponseB = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "MovePictureFolder_PictureMovedWithFolderMoveSuccesfull (2)" )}
			});
			// move the second folder to under the first folder
			var dataResponseC = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "Id", dataResponseB.Content.Id.ToString() ),
				new KeyValuePair<string,string>( "ParentFolderId", dataResponseA.Content.Id.ToString() )},
			});
			// Verify that the move was successful
			Assert.IsNotNull(dataResponseC);
			Assert.AreEqual(string.Empty, dataResponseC.Message);
			Assert.IsTrue(dataResponseC.Successful);
			// Delete the parent folder
			var parentFolder = dataResponseA.Content as Folder;
			parentFolder.IsDeleted = true;
			DAL.DataServer.Save(parentFolder);
			// Verify that the moved folder is no longer under the library
			var folder = dataResponseC.Content as Folder;
			Assert.AreNotEqual(authResponse.Content.Library.FolderId, folder.ParentFolderId);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.MovePictureFolder, 5, typeof(DataController))]
		public void MovePictureFolder_CopyFolderMovetoFolderSuccesfull()
		{
			var authController = new AuthenticationController();
			// Try to log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@email.com", password = "password" });
			// Verify successful authentication
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload a new folder
			var dataResponseA = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "MovePictureFolder_CopyFolderMovetoFolderSuccesfull" )}
			});
			// Upload a second folder
			var dataResponseB = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "MovePictureFolder_CopyFolderMovetoFolderSuccesfull (2)" )}
			});
			// Move folder B under folder A
			var dataResponseC = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "Id", dataResponseB.Content.Id.ToString() ),
				new KeyValuePair<string,string>( "ParentFolderId", dataResponseA.Content.Id.ToString() )},
			});
			// Verify successful move
			Assert.IsNotNull(dataResponseC);
			Assert.AreEqual(string.Empty, dataResponseC.Message);
			Assert.IsTrue(dataResponseC.Successful);
			// Delete the parent folder
			var parentFolder = dataResponseA.Content as Folder;
			parentFolder.IsDeleted = true;
			DAL.DataServer.Save(parentFolder);
			// Verify that the moved folder is not under the library anymore
			var folder = dataResponseC.Content as Folder;
			Assert.AreNotEqual(authResponse.Content.Library.FolderId, folder.ParentFolderId);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.RenamePictureFolder, 1, typeof(DataController))]
		public void RenamePictureFolder_AuthenticationFailed()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@sir.com", password = "password" });
			// Verify log in failed
			Assert.IsNotNull(authResponse);
			Assert.IsFalse(authResponse.Successful);
			var dataController = new DataController();
			// Attempt data upload
			var dataResponse = dataController.Upload(new UploadCriteria() { objectType = "", values = new List<KeyValuePair<string, string>>() });
			// Verify that the upload failed because the user was not authenticated
			Assert.IsNotNull(dataResponse);
			Assert.IsFalse(dataResponse.Successful);
			Assert.AreEqual("User must be authenticated", dataResponse.Message);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.RenamePictureFolder, 2, typeof(DataController))]
		public void RenamePictureFolder_RenameSamplePicturetoSampleB()
		{
			var authController = new AuthenticationController();
			// Log In
			var authResponse = authController.LogIn(new Credentials() { email = "test@email.com", password = "password" });
			// Verify successful log in
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload a new image
			var dataResponseA = dataController.Upload(new UploadCriteria()
			{
				objectType = "Image",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "FolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "Sample Picture" )}
			});
			// Chang ethe image's name
			var dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "Image",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "Id", dataResponseA.Content.Id.ToString() ),
				new KeyValuePair<string,string>( "Name", "Sample B" )}
			});
			// delete the image 
			var image = dataResponse.Content as Image;
			image.IsDeleted = true;
			DAL.DataServer.Save(image);
			// verify the response was successful
			Assert.IsNotNull(dataResponse);
			Assert.AreEqual(string.Empty, dataResponse.Message);
			Assert.IsTrue(dataResponse.Successful);
			// Verify the image values are correct
			Assert.IsNotNull(image);
			Assert.AreEqual(dataResponseA.Content.Id, dataResponse.Content.Id);
			Assert.AreEqual(authResponse.Content.Id, image.UserId);
			Assert.AreNotEqual(0, image.FolderId);
			Assert.AreEqual("Sample B", image.Name);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.RenamePictureFolder, 3, typeof(DataController))]
		public void RenamePictureFolder_RenameSampleFoldertoFolderB()
		{
			var authController = new AuthenticationController();
			// Log In
			var authResponse = authController.LogIn(new Credentials() { email = "test@email.com", password = "password" });
			// Verify authentication successful
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload a new folder
			var dataResponseA = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "Sample Folder" )}
			});
			// Change the name of the folder
			var dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "Id", dataResponseA.Content.Id.ToString() ),
				new KeyValuePair<string,string>( "Name", "Folder B" )}
			});
			// delete the folder
			var folder = dataResponse.Content as Folder;
			folder.IsDeleted = true;
			DAL.DataServer.Save(folder);
			// Verify update was successful
			Assert.IsNotNull(dataResponse);
			Assert.AreEqual(string.Empty, dataResponse.Message);
			Assert.IsTrue(dataResponse.Successful);
			// Verify that the folder values match the uploaded and updated values
			Assert.IsNotNull(folder);
			Assert.AreEqual(dataResponseA.Content.Id, dataResponse.Content.Id);
			Assert.AreEqual(authResponse.Content.Id, folder.UserId);
			Assert.AreNotEqual(0, folder.ParentFolderId);
			Assert.AreEqual("Folder B", folder.Name);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.RenamePictureFolder, 4, typeof(DataController))]
		public void RenamePictureFolder_RenamePicturetoSample()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@email.com", password = "password" });
			// Verify authentication successful
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload new image
			var dataResponseA = dataController.Upload(new UploadCriteria()
			{
				objectType = "Image",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "FolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "Picture" )}
			});
			// Upload a second image
			var dataResponseB = dataController.Upload(new UploadCriteria()
			{
				objectType = "Image",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "FolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "Sample" )}
			});
			// Change the name of the first image to the name of the second image
			var dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "Image",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "Id", dataResponseA.Content.Id.ToString() ),
				new KeyValuePair<string,string>( "Name", "Sample" )}
			});
			// Delete both images
			var image = dataResponseB.Content as Image;
			image.IsDeleted = true;
			DAL.DataServer.Save(image);

			image = dataResponse.Content as Image;
			image.IsDeleted = true;
			DAL.DataServer.Save(image);
			// Verify the name update was successful
			Assert.IsNotNull(dataResponse);
			Assert.AreEqual(string.Empty, dataResponse.Message);
			Assert.IsTrue(dataResponse.Successful);
			// Verify that the data is correct and the name was adjusted by the name resolver
			Assert.IsNotNull(image);
			Assert.AreEqual(dataResponseA.Content.Id, dataResponse.Content.Id);
			Assert.AreEqual(authResponse.Content.Id, image.UserId);
			Assert.AreNotEqual(0, image.FolderId);
			Assert.AreEqual("Sample (2)", image.Name);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.RenamePictureFolder, 5, typeof(DataController))]
		public void RenamePictureFolder_RenameFolderCopytoSamples()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@email.com", password = "password" });
			// Verify log in was successful
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload a new folder
			var dataResponseA = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "Folder" )}
			});
			// Upload a second folder
			var dataResponseB = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "Sample" )}
			});
			// Change the name of the first folder to the name of the second folder
			var dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "Id", dataResponseA.Content.Id.ToString() ),
				new KeyValuePair<string,string>( "Name", "Sample" )}
			});
			// Delete both folders
			var folder = dataResponseB.Content as Folder;
			folder.IsDeleted = true;
			DAL.DataServer.Save(folder);

			folder = dataResponse.Content as Folder;
			folder.IsDeleted = true;
			DAL.DataServer.Save(folder);
			// Verify the update was successful
			Assert.IsNotNull(dataResponse);
			Assert.AreEqual(string.Empty, dataResponse.Message);
			Assert.IsTrue(dataResponse.Successful);
			// Check the values of the updated folder and that the name was changed by the name resolver
			Assert.IsNotNull(folder);
			Assert.AreEqual(dataResponseA.Content.Id, dataResponse.Content.Id);
			Assert.AreEqual(authResponse.Content.Id, folder.UserId);
			Assert.AreNotEqual(0, folder.ParentFolderId);
			Assert.AreEqual("Sample (2)", folder.Name);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.DeletePictureFolder, 1, typeof(DataController))]
		public void DeletePictureFolder_AuthenticationFailed()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@sir.com", password = "password" });
			// Verify authentication failed
			Assert.IsNotNull(authResponse);
			Assert.IsFalse(authResponse.Successful);
			var dataController = new DataController();
			// Attempt to upload data
			var dataResponse = dataController.Upload(new UploadCriteria() { objectType = "", values = new List<KeyValuePair<string, string>>() });
			// Verify upload failed because of no authenticated user
			Assert.IsNotNull(dataResponse);
			Assert.IsFalse(dataResponse.Successful);
			Assert.AreEqual("User must be authenticated", dataResponse.Message);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.DeletePictureFolder, 2, typeof(DataController))]
		public void DeletePictureFolder_DeletePictureSample()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@email.com", password = "password" });
			// Verify authentication successful
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload a new image
			var dataResponseA = dataController.Upload(new UploadCriteria()
			{
				objectType = "Image",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "FolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "DeletePictureSample" )}
			});
			// Delete the image through the upload method
			var dataResponseB = dataController.Upload(new UploadCriteria()
			{
				objectType = "Image",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "Id", dataResponseA.Content.Id.ToString() ),
				new KeyValuePair<string,string>( "IsDeleted", "true" )}
			});
			// Attempt to download the image
			var dataResponseC = dataController.Download("Image",
				new Predicate("Id", dataResponseA.Content.Id.ToString(), PredicateOperator.EqualTo));
			// Verify that the download was successful but had no items (image was deleted)
			Assert.IsNotNull(dataResponseC);
			Assert.AreEqual(string.Empty, dataResponseC.Message);
			Assert.IsTrue(dataResponseC.Successful);
			Assert.AreEqual(0, dataResponseC.Content.Count());
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.DeletePictureFolder, 3, typeof(DataController))]
		public void DeletePictureFolder_DeleteFolder()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@email.com", password = "password" });
			// Verify authentication successful
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload a new folder
			var dataResponseA = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "Name", "DeleteFolder" )}
			});
			// Delete the folder through the upload request
			var dataResponseB = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "Id", dataResponseA.Content.Id.ToString() ),
				new KeyValuePair<string,string>( "IsDeleted", "true" )}
			});
			// Attempt to download the folder
			var dataResponseC = dataController.Download("Folder",
				new Predicate("Id", dataResponseA.Content.Id.ToString(), PredicateOperator.EqualTo));
			// Verify that the request was successful but no results were found (folder deleted)
			Assert.IsNotNull(dataResponseC);
			Assert.AreEqual(string.Empty, dataResponseC.Message);
			Assert.IsTrue(dataResponseC.Successful);
			Assert.AreEqual(0, dataResponseC.Content.Count());
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.AddFavorite, 1, typeof(DataController))]
		public void AddFavorite_AuthenticationFailed()
		{
			var authController = new AuthenticationController();
			// Attempt authentication
			var authResponse = authController.LogIn(new Credentials() { email = "test@sir.com", password = "password" });
			// Verify authentication failed
			Assert.IsNotNull(authResponse);
			Assert.IsFalse(authResponse.Successful);
			var dataController = new DataController();
			// Attempt to upload data
			var dataResponse = dataController.Upload(new UploadCriteria() { objectType = "", values = new List<KeyValuePair<string, string>>() });
			// Verify that the upload failed because there was no authenticated user
			Assert.IsNotNull(dataResponse);
			Assert.IsFalse(dataResponse.Successful);
			Assert.AreEqual("User must be authenticated", dataResponse.Message);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.AddFavorite, 2, typeof(DataController))]
		public void AddFavorite_AddFavoritePicture()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@email.com", password = "password" });
			// Verify authentication successful
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload a new favorite image
			var dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "FavoriteImage",
				values = new List<KeyValuePair<string, string>>() { 
				new KeyValuePair<string,string>( "ImageId", "1" )}
			});
			// Verify the response was successful
			Assert.IsNotNull(dataResponse);
			Assert.AreEqual(string.Empty, dataResponse.Message);
			Assert.IsTrue(dataResponse.Successful);
			// Verify the user id and image id were populated
			var favImage = dataResponse.Content as FavoriteImage;
			Assert.AreEqual(1, favImage.ImageId);
			Assert.AreEqual(1, favImage.UserId);
			// Delete the favorite image
			favImage.IsDeleted = true;
			DAL.DataServer.Save<FavoriteImage>(favImage);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.AddFavorite, 3, typeof(DataController))]
		public void AddFavorite_AddPicturetoFavoritesThatIsAlreadyAFavorite()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@email.com", password = "password" });
			// Verify authentication successful
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload a new favorite image
			var dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "FavoriteImage",
				values = new List<KeyValuePair<string, string>>() {  
				new KeyValuePair<string,string>( "ImageId", "2" )}
			});
			// Reupload the same favorite image
			dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "FavoriteImage",
				values = new List<KeyValuePair<string, string>>() {  
				new KeyValuePair<string,string>( "Id", dataResponse.Content.Id.ToString() ),
				new KeyValuePair<string,string>( "UserId", authResponse.Content.Id.ToString() ),
				new KeyValuePair<string,string>( "ImageId", "2" )}
			});
			// Check that the response was successful
			Assert.IsNotNull(dataResponse);
			Assert.AreEqual(string.Empty, dataResponse.Message);
			Assert.IsTrue(dataResponse.Successful);
			// Verify that the favorite image data matches correctly
			var favImage = dataResponse.Content as FavoriteImage;
			Assert.AreEqual(2, favImage.ImageId);
			Assert.AreEqual(1, favImage.UserId);
			// delete the favorite image
			favImage.IsDeleted = true;
			DAL.DataServer.Save<FavoriteImage>(favImage);
		}


		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.ViewFavorites, 1, typeof(DataController))]
		public void ViewFavorites_AuthenticationFailed()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@sir.com", password = "password" });
			// Verify authentication failed
			Assert.IsNotNull(authResponse);
			Assert.IsFalse(authResponse.Successful);
			var dataController = new DataController();
			// Attempt download
			var dataResponse = dataController.Download("FavoriteImage", null);
			// Verify the download failed because there was no authenticated user
			Assert.IsNotNull(dataResponse);
			Assert.IsFalse(dataResponse.Successful);
			Assert.AreEqual("User must be authenticated", dataResponse.Message);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.ViewFavorites, 2, typeof(DataController))]
		public void ViewFavorites_ViewFavoriteImageSuccessful()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "matt@sir.com", password = "password" });
			// Verify authentication successful
			Assert.IsNotNull(authResponse);
			Assert.AreEqual("Authentication Successful", authResponse.Message);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload a new favorite image
			var uploadResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "FavoriteImage",
				values = new List<KeyValuePair<string, string>>() {  
				new KeyValuePair<string,string>( "ImageId", "3" )}
			});
			// Verify the upload was successful
			Assert.IsNotNull(uploadResponse);
			Assert.AreEqual(string.Empty, uploadResponse.Message);
			Assert.IsTrue(uploadResponse.Successful);
			// Verify the favorite image data
			var favImage = uploadResponse.Content as FavoriteImage;
			Assert.AreEqual(3, favImage.ImageId);
			Assert.AreEqual(6, favImage.UserId);
			// Download the favorite images
			var dataResponse = dataController.Download("FavoriteImage",
				new Common.Condition.Predicate("FavoriteImage.UserId", authResponse.Content.Id, Common.Condition.PredicateOperator.EqualTo));
			// delete the favorite image
			favImage.IsDeleted = true;
			DAL.DataServer.Save<FavoriteImage>(favImage);
			// Verify the download was successful and returned at least one result
			Assert.IsNotNull(dataResponse);
			Assert.IsTrue(dataResponse.Successful);
			Assert.IsTrue(((IList)dataResponse.Content).Count >= 1);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.RemoveFavorite, 1, typeof(DataController))]
		public void RemoveFavorite_AuthenticationFailed()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@sir.com", password = "password" });
			// Verify authentication failed
			Assert.IsNotNull(authResponse);
			Assert.IsFalse(authResponse.Successful);
			var dataController = new DataController();
			// Upload request
			var dataResponse = dataController.Upload(new UploadCriteria() { objectType = "", values = new List<KeyValuePair<string, string>>() });
			// Verify upload failed because there was no authenticated user
			Assert.IsNotNull(dataResponse);
			Assert.IsFalse(dataResponse.Successful);
			Assert.AreEqual("User must be authenticated", dataResponse.Message);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.RemoveFavorite, 2, typeof(DataController))]
		public void RemoveFavorite_RemoveFavoritePicture()
		{
			var authController = new AuthenticationController();
			// log in
			var authResponse = authController.LogIn(new Credentials() { email = "paul.w@sir.com", password = "password" });
			// Verify authentication was successful
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload a new favorite image
			var dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "FavoriteImage",
				values = new List<KeyValuePair<string, string>>() {  
				new KeyValuePair<string,string>( "ImageId", "1" )}
			});
			// Delete the favorite image through the upload process
			dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "FavoriteImage",
				values = new List<KeyValuePair<string, string>>() {  
				new KeyValuePair<string,string>( "Id", dataResponse.Content.Id.ToString()),
				new KeyValuePair<string,string>( "ImageId", "1" ),
				new KeyValuePair<string,string>( "IsDeleted", "true" )}
			});
			// Verify the delete upload was successful and had no content
			Assert.IsNotNull(dataResponse);
			Assert.AreEqual(string.Empty, dataResponse.Message);
			Assert.IsTrue(dataResponse.Successful);
			Assert.IsNull(dataResponse.Content);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.RemoveFavorite, 3, typeof(DataController))]
		public void RemoveFavorite_RemovePicturetFromFavoritesThatIsNotAlreadyAFavorite()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "paul_c@sir.com", password = "password" });
			// Verify authentication successful
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			var dataController = new DataController();
			// Upload the delete of an object that doesn't exist
			var dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "FavoriteImage",
				values = new List<KeyValuePair<string, string>>() {  
				new KeyValuePair<string,string>( "ImageId", "1" ),
				new KeyValuePair<string,string>( "IsDeleted", "true" )}
			});
			// Verify that the response was successful and no contents were returned
			Assert.IsNotNull(dataResponse);
			Assert.AreEqual(string.Empty, dataResponse.Message);
			Assert.IsTrue(dataResponse.Successful);
			Assert.IsNull(dataResponse.Content);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.ChangePrivacySetting, 1, typeof(DataController))]
		public void ChangePrivacySetting_AuthenticationFailed()
		{
			var authController = new AuthenticationController();
			// Log in
			var authResponse = authController.LogIn(new Credentials() { email = "test@sir.com", password = "password" });
			// Verify the authentenication failed
			Assert.IsNotNull(authResponse);
			Assert.IsFalse(authResponse.Successful);
			var dataController = new DataController();
			// Upload data
			var dataResponse = dataController.Upload(new UploadCriteria() { objectType = "", values = new List<KeyValuePair<string, string>>() });
			// VErify upload failed because the user was not authenticated
			Assert.IsNotNull(dataResponse);
			Assert.IsFalse(dataResponse.Successful);
			Assert.AreEqual("User must be authenticated", dataResponse.Message);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.ChangePrivacySetting, 2, typeof(DataController))]
		public void ChangePrivacySetting_Image()
		{
			var authController = new AuthenticationController();
			// log in
			var authResponse = authController.LogIn(new Credentials() { email = "dania@sir.com", password = "password" });
			// Verify authentication successful
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			// Upload a new image
			var imageController = new ImageController();
			// Initialize the request
			imageController.Request = new System.Net.Http.HttpRequestMessage();
			imageController.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
			// Get the full file name
			var fileName = Path.Combine(Directory.GetCurrentDirectory(), "..\\..\\..\\SIR.Web.Tests\\Content\\ChangePrivacySetting_Image.jpg");
			var formDataContent = new MultipartFormDataContent();
			// Read the bytes
			var fileContent = new StreamContent(new MemoryStream(System.IO.File.ReadAllBytes(fileName)));
			// Build the content
			fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/JPEG");
			formDataContent.Add(fileContent, "\"file\"", fileName);
			// set the request content
			imageController.Request.Content = formDataContent;
			// Get results from the post
			var result = imageController.PostFormData().Result;
			// Get the content of the result
			var content = ((ObjectContent<List<Response<Image>>>)(result.Content)).ReadAsAsync<List<Response<Image>>>().Result;
			// Verify the upload was successful
			Assert.IsNotNull(content);
			Assert.AreEqual(content[0].Message, "");
			Assert.IsTrue(content[0].Successful);

			var dataController = new DataController();
			// Set the new image to private trhough the upload controller
			var dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "Image",
				values = new List<KeyValuePair<string, string>>() 
			{ 
				new KeyValuePair<string,string>( "Id", content[0].Content.Id.ToString() ),
				new KeyValuePair<string,string>( "IsPrivate", "true" )}
			});
			// verify that the update was successful
			Assert.IsNotNull(dataResponse);
			Assert.IsTrue(dataResponse.Successful);
			var image = dataResponse.Content as IImage;
			// Verify the image is private
			Assert.IsTrue(image.IsPrivate);
			// Search by the uploaded image
			var searchController = new SearchController();
			var searchResponse = searchController.Image(100, image.Id);
			// Delete the new image
			content[0].Content.IsDeleted = true;
			DAL.DataServer.Save(content[0].Content as Image);
			// Verify the search result was successful but returned no results
			Assert.IsNotNull(searchResponse);
			Assert.IsNotNull(searchResponse.Content);
			Assert.AreEqual(0, ((ICollection)searchResponse.Content).Count);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.ChangePrivacySetting, 3, typeof(DataController))]
		public void ChangePrivacySetting_Folder()
		{
			var authController = new AuthenticationController();
			// log in
			var authResponse = authController.LogIn(new Credentials() { email = "dania@sir.com", password = "password" });
			// Verify authentication successful
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);

			var dataController = new DataController();
			// Upload a new folder as private
			var dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() 
			{ 
				new KeyValuePair<string,string>( "ParentFolderId", authResponse.Content.Library.FolderId.ToString() ),
				new KeyValuePair<string,string>( "IsPrivate", "true" )}
			});
			// Verify the folder was created successfully
			Assert.IsNotNull(dataResponse);
			Assert.AreEqual(string.Empty, dataResponse.Message);
			Assert.IsTrue(dataResponse.Successful);
			// Get the folder id
			var folderId = dataResponse.Content.Id;

			// Upload a new image
			var imageController = new ImageController();
			// Initialize the request
			imageController.Request = new System.Net.Http.HttpRequestMessage();
			imageController.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
			// Get the full file name
			var fileName = Path.Combine(Directory.GetCurrentDirectory(), "..\\..\\..\\SIR.Web.Tests\\Content\\ChangePrivacySetting_Image.jpg");
			var formDataContent = new MultipartFormDataContent();
			// Read the bytes
			var fileContent = new StreamContent(new MemoryStream(System.IO.File.ReadAllBytes(fileName)));
			// Build the content
			fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/JPEG");
			formDataContent.Add(fileContent, "\"file\"", fileName);
			// set the request content
			imageController.Request.Content = formDataContent;
			// Get results from the post
			var result = imageController.PostFormData().Result;
			// Get the content of the result
			var content = ((ObjectContent<List<Response<Image>>>)(result.Content)).ReadAsAsync<List<Response<Image>>>().Result;
			// Verify the upload was successful
			Assert.IsNotNull(content);
			Assert.AreEqual(content[0].Message, "");
			Assert.IsTrue(content[0].Successful);
			// Set the folder to private
			dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>()
			{ 
				new KeyValuePair<string,string>( "Id", folderId.ToString() ),
				new KeyValuePair<string,string>( "IsPrivate", "true" )}
			});
			// Verify the update was successful
			Assert.IsNotNull(dataResponse);
			Assert.AreEqual(string.Empty, dataResponse.Message);
			Assert.IsTrue(dataResponse.Successful);
			var folder = dataResponse.Content as Folder;
			// Verify the folder is now private
			Assert.IsTrue(folder.IsPrivate);
			// Search for the image that was uploaded
			var searchController = new SearchController();
			var searchResponse = searchController.Image(100, content[0].Content.Id);
			// Delete the new image
			folder.IsDeleted = true;
			DAL.DataServer.Save(folder);
			// Check that the search was successful but returned no results
			Assert.IsNotNull(searchResponse);
			Assert.IsNotNull(searchResponse.Content);
			Assert.AreEqual(0, ((ICollection)searchResponse.Content).Count);
		}

		[TestMethod]
		[SIRTestCategory(typeof(Area.UserProfile), Area.UserProfile.ChangePrivacySetting, 4, typeof(DataController))]
		public void ChangePrivacySetting_Library()
		{
			var authController = new AuthenticationController();
			// log in
			var authResponse = authController.LogIn(new Credentials() { email = "dania@sir.com", password = "password" });
			// Verify authentication successful
			Assert.IsNotNull(authResponse);
			Assert.IsTrue(authResponse.Successful);
			// set the folder Id to the library
			var folderId = authResponse.Content.Library.FolderId.ToString();
			var dataController = new DataController();

			// Upload a new image
			var imageController = new ImageController();
			// Initialize the request
			imageController.Request = new System.Net.Http.HttpRequestMessage();
			imageController.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
			// Get the full file name
			var fileName = Path.Combine(Directory.GetCurrentDirectory(), "..\\..\\..\\SIR.Web.Tests\\Content\\ChangePrivacySetting_Image.jpg");
			var formDataContent = new MultipartFormDataContent();
			// Read the bytes
			var fileContent = new StreamContent(new MemoryStream(System.IO.File.ReadAllBytes(fileName)));
			// Build the content
			fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/JPEG");
			formDataContent.Add(fileContent, "\"file\"", fileName);
			// set the request content
			imageController.Request.Content = formDataContent;
			// Get results from the post
			var result = imageController.PostFormData().Result;
			// Get the content of the result
			var content = ((ObjectContent<List<Response<Image>>>)(result.Content)).ReadAsAsync<List<Response<Image>>>().Result;
			// Verify the upload was successful
			Assert.IsNotNull(content);
			Assert.AreEqual(content[0].Message, "");
			Assert.IsTrue(content[0].Successful);
			// Set the library to private
			var dataResponse = dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() 
			{ 
				new KeyValuePair<string,string>( "Id", folderId ),
				new KeyValuePair<string,string>( "IsPrivate", "true" )}
			});
			// Verify the update was successful
			Assert.IsNotNull(dataResponse);
			Assert.IsTrue(dataResponse.Successful);
			var folder = dataResponse.Content as Folder;
			// Verify the folder is now private
			Assert.IsTrue(folder.IsPrivate);
			// Search for the image
			var image = content[0].Content as Image;
			var searchController = new SearchController();
			var searchResponse = searchController.Image(100, image.Id);
			// Change the library back to public
			dataController.Upload(new UploadCriteria()
			{
				objectType = "Folder",
				values = new List<KeyValuePair<string, string>>() 
			{ 
				new KeyValuePair<string,string>( "Id", folderId ),
				new KeyValuePair<string,string>( "IsPrivate", "false" )}
			});
			// Delete the new image
			content[0].Content.IsDeleted = true;
			DAL.DataServer.Save(image);
			// Verify that no images were found in the search
			Assert.IsNotNull(searchResponse);
			Assert.IsNotNull(searchResponse.Content);
			Assert.AreEqual(0, ((ICollection)searchResponse.Content).Count);
		}
	}
}
